// pages/ProductDetail/ProductDetail.js
const {
  Detail,
  GoShop
} = require('../../utils/api')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    ArrInfo: [],
    Content: '',
    show: false,
    abc: 'a',
    Top1: 0,
    Top2: 509.5038146972656,
    Value: 1,
    ShopL:0,

  },
  onPageScroll(e) {
    // console.log(e.scrollTop);
    if (e.scrollTop == 0) {
      this.setData({
        abc: 'a'
      })
    } else if (e.scrollTop >= 509.5038146972656) {
      this.setData({
        abc: 'b'
      })
    }
  },
  onClick(e) {
    console.log(e.detail);
    if (e.detail.index == 1) {
      wx.pageScrollTo({
        scrollTop: 509.5038146972656
      })

    } else if (e.detail.index == 2) {
      wx.pageScrollTo({
        scrollTop: 2708.800048828125
      })
    } else {
      wx.pageScrollTo({
        scrollTop: 0
      })
    }
  },
  onChange(e) {
    console.log(e.detail);
  },
  add(e) {
    this.setData({
      show: true
    })
    console.log(this.data.ArrInfo);
  },
  // 加入购物车
  adds(){
    GoShop({
      goodsId:this.data.ArrInfo.basicInfo.id,
      number:this.data.ArrInfo.basicInfo.minBuyNumber,
      token:wx.getStorageSync('token')
    }).then((res)=>{
      console.log(res);
      wx.showToast({
        title: '成功加入购物车',
      })
      console.log(res);
      wx.setStorageSync('shop',res.items.length)
      // console.log();
      this.onClose()
    })
  },
  onClose() {
    this.setData({
      show: false
    })
  },
  // 数量加
  Jia(e){
    // console.log(e);
    let val = e.currentTarget.dataset.val
    val.basicInfo.minBuyNumber++
    this.setData({
      ArrInfo:val 
    })
    // console.log(this.data.ArrInfo);
  },
  // 数量减
  Jian(e){
    // console.log(e);
    let val = e.currentTarget.dataset.val
    val.basicInfo.minBuyNumber--
    this.setData({
      ArrInfo:val 
    })
    // console.log(this.data.ArrInfo);
  },
  // 跳转到购物车页面
  Jump(){
    wx.switchTab({
      url: '/pages/Shopping Cart/Shopping Cart',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(options);
    let Ids = options.id
    Detail({
      id: Ids
    }).then((res) => {
      console.log(res);
      this.setData({
        ArrInfo: res,
        Content: res.content.replace(/\<img/gi, '<img style="max-width:100%;height:auto"')
      })

      console.log(this.data.ArrInfo);

    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    // wx.getStorageSync('shop')
    this.setData({
      ShopL: wx.getStorageSync('shop')
    })
    console.log(this.data.ShopL);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})